package com.example.idachuappone.person.member;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.ui.MyListView;
import com.example.idachuappone.person.entity.MemberTypee;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class MemberTypeActivity extends BaseActivity implements OnClickListener {
	private MyMemberTypeAdapter adapter;
	private Button btn_back;
	private BitmapUtils bu;
	private Dialog dialogLoading;
	private boolean isMemberRecharege;
	private boolean ispayprompty;
	private List<MemberTypee> list;
	private LinearLayout ll_no_network;
	private MyListView mlv_main;
	private TextView tv_title;

	private class MyMemberTypeAdapter extends BaseAdapter {
		private Context context;
		private List<MemberTypee> list;

		class AnonymousClass_1 implements OnClickListener {
			private final/* synthetic */int val$position;

			AnonymousClass_1(int i) {
				this.val$position = i;
			}

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MyMemberTypeAdapter.this.context, MemberDetailActivity.class);
				intent.putExtra("memberTypee", MyMemberTypeAdapter.this.list.get(this.val$position));
				intent.putExtra("ispayprompty", MemberTypeActivity.this.ispayprompty);
				MemberTypeActivity.this.startActivityForResult(intent, 555);
			}
		}

		class AnonymousClass_2 implements OnClickListener {
			private final/* synthetic */int val$position;

			AnonymousClass_2(int i) {
				this.val$position = i;
			}

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MyMemberTypeAdapter.this.context, MemberDetailActivity.class);
				intent.putExtra("memberTypee", MyMemberTypeAdapter.this.list.get(this.val$position));
				intent.putExtra("ispayprompty", MemberTypeActivity.this.ispayprompty);
				MemberTypeActivity.this.startActivityForResult(intent, 555);
			}
		}

		public MyMemberTypeAdapter(Context context) {
			this.context = context;
		}

		public void setList(List<MemberTypee> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}

		@Override
		public Object getItem(int position) {
			return this.list == null ? null : (MemberTypee) this.list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LinearLayout.inflate(this.context, R.layout.simple_member_type_list_item, null);
				holder.img_member = (ImageView) convertView.findViewById(R.id.img_member);
				holder.btn_member = (Button) convertView.findViewById(R.id.btn_member);
				holder.rl_bg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			MemberTypee memberTypee = this.list.get(position);
			if (MemberTypeActivity.this.isMemberRecharege) {
				holder.btn_member.setText("\u5145\u503c");
			} else {
				holder.btn_member.setText("\u5f00\u901a");
			}
			if (Integer.parseInt(memberTypee.getDegree()) == 1) {
				holder.rl_bg.setBackgroundResource(R.drawable.experience_card);
				holder.btn_member.setBackgroundResource(R.drawable.img_two_member_1);
				holder.btn_member.setTextColor(this.context.getResources().getColor(R.color.color_two_member_1));
			} else if (Integer.parseInt(memberTypee.getDegree()) == 2) {
				holder.rl_bg.setBackgroundResource(R.drawable.general_card);
				holder.btn_member.setBackgroundResource(R.drawable.img_two_member_2_2);
				holder.btn_member.setTextColor(this.context.getResources().getColor(R.color.color_two_member_2_2));
			} else if (Integer.parseInt(memberTypee.getDegree()) == 3) {
				holder.rl_bg.setBackgroundResource(R.drawable.sliver_card);
				holder.btn_member.setBackgroundResource(R.drawable.img_two_member_2);
				holder.btn_member.setTextColor(this.context.getResources().getColor(R.color.color_two_member_2));
			} else if (Integer.parseInt(memberTypee.getDegree()) == 4) {
				holder.rl_bg.setBackgroundResource(R.drawable.god_card);
				holder.btn_member.setBackgroundResource(R.drawable.img_two_member_3);
				holder.btn_member.setTextColor(this.context.getResources().getColor(R.color.color_two_member_3));
			}
			holder.btn_member.setOnClickListener(new AnonymousClass_1(position));
			holder.rl_bg.setOnClickListener(new AnonymousClass_2(position));
			MemberTypeActivity.this.bu.display(holder.img_member, memberTypee.getImg2());
			return convertView;
		}
	}

	private class ViewHolder {
		Button btn_member;
		ImageView img_member;
		RelativeLayout rl_bg;

		private ViewHolder() {
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member_type);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.ispayprompty = getIntent().getBooleanExtra("ispayprompty", false);
		this.isMemberRecharege = getIntent().getBooleanExtra("isMemberRecharege", false);
		initView();
	}

	private void initView() {
		this.tv_title = (TextView) findViewById(R.id.tv_title);
		if (this.isMemberRecharege) {
			this.tv_title.setText("\u9009\u62e9\u5145\u503c\u4f1a\u5458");
		} else {
			this.tv_title.setText("\u9009\u62e9\u4f1a\u5458");
		}
		this.ll_no_network = (LinearLayout) findViewById(R.id.ll_no_network);
		this.ll_no_network.setOnClickListener(this);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.mlv_main = (MyListView) findViewById(R.id.mlv_main);
		this.adapter = new MyMemberTypeAdapter(this);
		this.mlv_main.setAdapter(this.adapter);
		initData();
	}

	private void initData() {
		if (CheckUtil.checkNet(this)) {
			NetUtil.get(this, Constant.MEMBER_LIST, 100, new RequestCallBack<String>() {
				@Override
				public void onStart() {
					MemberTypeActivity.this.dialogLoading.show();
				}

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					MemberTypeActivity.this.dialogLoading.dismiss();
					MemberTypeActivity.this.ll_no_network
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						if (jsonObject.getInt("code") == 10000) {
							MemberTypeActivity.this.list = new ArrayList();
							JSONArray jsonArray = jsonObject.getJSONArray("data");
							for (int i = 0; i < jsonArray.length(); i++) {
								JSONObject object = jsonArray.getJSONObject(i);
								MemberTypee memberTypee = new MemberTypee();
								memberTypee.parseJson(object);
								MemberTypeActivity.this.list.add(memberTypee);
							}
							MemberTypeActivity.this.initAdapter();
							return;
						}
						MainToast.show(MemberTypeActivity.this, jsonObject.getString("error"), 0);
					} catch (JSONException e) {
						e.printStackTrace();
						MainToast.show(MemberTypeActivity.this,
								MemberTypeActivity.this.getResources().getString(R.string.app_error), 0);
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					MemberTypeActivity.this.dialogLoading.dismiss();
					MemberTypeActivity.this.ll_no_network.setVisibility(0);
				}
			});
		} else {
			this.ll_no_network.setVisibility(0);
		}
	}

	private void initAdapter() {
		this.adapter.setList(this.list);
		this.adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.ll_no_network /* 2131165232 */:
			initData();
		default:
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 555 && resultCode == -1) {
			setResult(-1);
			finish();
		}
	}
}
